package com.qd.panda.collect;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.dto.DelDTO;
import com.cdqidi.util.page.PageUtil;
import com.cdqidi.valid.group.DefaultGroup;
import com.qd.common.panda.domain.entity.collect.UserCollectDTO;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.collect.UserCollectService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * APP_用户收藏
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/userCollect/")
@Slf4j
public class UserCollectController {
    private final UserCollectService userCollectService;

    /**
     * 收藏
     *
     * @param dto 页面对象
     * @return 页面对象
     */
    @PostMapping(value = "/collect")
    public UserCollectDTO collect(@Validated({DefaultGroup.class}) @RequestBody UserCollectDTO dto) {
        dto.setAddTime(LocalDateTime.now());
        if (null == dto.getUserId()) {
            Optional.ofNullable(SecurityUtils.getLoginUser()).ifPresent(loginUser -> dto.setUserId(loginUser.getUserId()));
        }
        return userCollectService.save(dto);
    }

    /**
     * 取消收藏
     *
     * @return 页面对象
     */
    @DeleteMapping(value = "/cancelCollect")
    public ApiResultWrapper<Object> cancelCollect(@Validated @RequestBody DelDTO<String> delDTO) {
        final List<String> ids = delDTO.getIds();
        if (ids.size() == 1) {
            userCollectService.removeById(ids.get(0));
        } else {
            return ApiResult.error("不支持批量操作");
        }
        return ApiResult.success("删除成功");
    }

    /**
     * 列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<UserCollectDTO> page(UserCollectDTO dto) {
        if (!StringUtils.hasLength(dto.getUserId())) {
            dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        }
        return userCollectService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 收藏的点
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/list")
    public List<UserCollectDTO> list(UserCollectDTO dto) {
        if (!StringUtils.hasLength(dto.getUserId())) {
            dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        }
        final IPage<UserCollectDTO> page = userCollectService.page(PageUtil.buildPage(dto), dto);
        final List<UserCollectDTO> records = page.getRecords();
        if (!records.isEmpty()) {
            records.forEach(userCollectDTO -> Optional.ofNullable(userCollectDTO.getCarParkInfo()).ifPresent(carParkInfoDTO -> {
                userCollectDTO.setParkIdDisplay(carParkInfoDTO.getName());
                carParkInfoDTO.freeData();
            }));
            return records;
        }
        return Collections.emptyList();
    }
}